package com.hornet.manager.system.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hornet.commons.web.BaseController;
import com.hornet.commons.web.model.AjaxResponse;
import com.hornet.manager.system.model.Menu;
import com.hornet.manager.system.service.MenuService;

@Controller
@RequestMapping("/manager/system/menu")
public class MenuController extends BaseController {

	@Autowired
	private MenuService service;
	
	/**
	 * 主页
	 * @return
	 */
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main(Model dto) {
		return viewName("main");
	}
	
	/**
	 * 表单页面
	 * @param id
	 * @param dto
	 * @return
	 */
	@RequestMapping(value="/form", method=RequestMethod.GET)
	public String form(Long id, Model dto) {
		if(null != id) {
			dto.addAttribute("id", id);
		}
		return viewName("form");
	}
	
	
	/**
	 * 添加
	 * @param dto
	 * @return
	 */
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse add(Menu dto) {
		return this.service.add(dto);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delete/{id}", method=RequestMethod.DELETE)
	public @ResponseBody AjaxResponse delete(@PathVariable Long id) {
		return this.service.delete(id);
	}
	
	/**
	 * 修改
	 * @param dto
	 * @return
	 */
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public @ResponseBody AjaxResponse update(Menu dto) {
		return this.service.update(dto);
	}
	
	/**
	 * 根据ID获取数据
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/get/{id}", method=RequestMethod.GET)
	public @ResponseBody Menu get(@PathVariable Long id) {
		return this.service.selectByKey(id);
	}
	
	/**
	 * 系统左侧菜单
	 * 获取所有一级目录
	 * @return
	 */
	@RequestMapping(value="/getAllMenusViewCatalog", method=RequestMethod.GET)
	public @ResponseBody List<Menu> getAllMenusViewCatalog() {
		return this.service.findAllMenuViewByTopCatalog();
	}
	
	/**
	 * 系统左侧菜单树
	 * 根据目录ID获取所有的子菜单和子目录
	 * @param catelog_id
	 * @return
	 */
	@RequestMapping(value="/getAllMenusViewChildren/{catalog_id}", method=RequestMethod.GET)
	public @ResponseBody List<Menu> getAllMenusViewChildren(@PathVariable Integer catalog_id) {
		return this.service.findMenuChildrenViewByPid(catalog_id);
	}
	
	/**
	 * 菜单管理
	 * @param dto
	 * @return
	 */
	@RequestMapping(value="/treegrid", method=RequestMethod.GET)
	public @ResponseBody List<Menu> treegrid(Menu dto) {
		return this.service.treegrid();
	}
	
	/**
	 * 获取所有目录
	 * @return
	 */
	@RequestMapping(value="/findMenuChildByCatalog", method=RequestMethod.GET)
	public @ResponseBody List<Menu> findMenuChildByCatalog() {
		return this.service.findMenuChildByCatalog();
	}
	
	/**
	 * 根据访问地址查询
	 * @param location
	 * @return
	 */
	@RequestMapping(value="/getLocation", method=RequestMethod.GET)
	public @ResponseBody Menu getLocation(String location) {
		return this.service.getLocation(location);
	}
}
